<?php
namespace app\manage\controller;

use think\Controller;
use think\Request;
use think\Validate;
use think\Db;
use Config;

/**租客管理
 * Class UnitBuilding
 * @package app\manage\controller
 */
class TenantManagement extends Controller{

    /**
     * 租客显示
     * @return \think\response\Json
     */
    public function TenantList(){
        $model=new \app\manage\model\TenantManagement();
        $date=$model->index();
        returnJson(200,"成功",$date)->send();die;
    }

    /**
     * 租客添加
     * @return \think\response\Json
     */
	public function TenantAdd(){
        $model=new \app\manage\model\TenantManagement();
        //get的时候显示后台设置的栋数和单元楼post的时候添加
        if(Request::instance()->isGet()){
            $model=new \app\manage\model\TenantManagement();
            $date=$model->templateList();
            returnJson(200,"成功",$date)->send();die;
        }else{
            $data = input();
            $validate = new Validate([
                ['tenant_name', 'require', '姓名不能为空'],
                ['tenant_tel', 'require|max:11|/^1[3-8]{1}[0-9]{9}$/', '手机号不能为空|手机号长度必须是11位|手机号格式不正确'],
                ['tenant_front', 'require', '身份证正面不能为空'],
                ['tenant_sex', 'require', '性别不能为空'],
                ['tenant_back', 'require', '身份证反面不能为空'],
                ['tenant_famous_race', 'require', '名族不能为空'],
                ['tenant_address', 'require', '地址不能为空']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            //判断手机号是否重复
            $phone=trim( $data['tenant_idcard'] );
            $token=trim( $data['token'] );
            $i=$model->tenantPhones($phone,$token);
            if($i!=null){
                return json(["code"=>-416,"msg"=>'此租客已存在']);
            }
            $date=$model->tenantAdd($data);
            if($date){
                returnJson(200,"成功",$date)->send();die;
            }else{
                returnJson(-412,"添加失败")->send();die;
            }
        }

	}

    /**
     * 租客修改
     * @return \think\response\Json
     */
    public function TenantUpdate(){
        $model=new \app\manage\model\TenantManagement();
        //get的时候显示需要修改的单元楼和单元楼post的时候修改
        if(Request::instance()->isGet()){
            $input = input();
            $h_id = trim( $input['tenant_id'] );
            $data=$model->tenantOne($h_id);
            $data['admin_user']=$model->templateList();
            //判断后台有没有设置单元楼栋数
            if($data==null){
                returnJson(-413,"请选择正确的住房")->send();die;
            }else{
                returnJson(200,"成功",$data)->send();die;
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['tenant_name', 'require', '姓名不能为空'],
                ['tenant_tel', 'require|max:11|/^1[3-8]{1}[0-9]{9}$/', '手机号不能为空|手机号长度必须是11位|手机号格式不正确'],
                ['tenant_front', 'require', '身份证正面不能为空'],
                ['tenant_sex', 'require', '性别不能为空'],
                ['tenant_back', 'require', '身份证反面不能为空'],
                ['tenant_famous_race', 'require', '名族不能为空'],
                ['tenant_address', 'require', '地址不能为空']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }
            //判断楼层房号是否重复

            $i=$model->tenantPhone($data);
            if($i!=null){
                return json(["code"=>-416,"msg"=>'该用户已存在']);
            }
            $date=$model->TenantUpdate($data);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-414,"修改失败")->send();die;
            }
        }
    }
    /**
     * 用户显示
     * @return \think\response\Json
     */
    public function tenantUser(){
        $model=new \app\manage\model\TenantManagement();
        $date=$model->tenantUser();
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 禁用用户和解禁
     * @return \think\response\Json
     */
    public function userStatus(){
        $model=new \app\manage\model\TenantManagement();
        $data = input();
        $validate = new Validate([
            ['id', 'require', '请选择用户'],
            ['type', 'require|number', '非法操作|非法操作']
        ]);
        if(!$validate->check($data))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }
        $date=$model->userStatus($data);
        if($date){
            returnJson(200,"成功")->send();die;
        }else{
            returnJson(-456,"失败")->send();die;
        }

    }

}